import {HttpRequest, HttpResponse} from '@angular/common/http';
import {MockData} from 'geminis-ng-dev';
import {GnMockData} from "../../projects/geminis-ng-dev/src/mock";

@GnMockData('/testData')
export class TestMockData implements MockData {

  build(request: HttpRequest<any>): HttpResponse<any> {
    const response = new HttpResponse<any>({body: {}});
    if (request.method === 'POST') {
      const pageIndex = request.body.pageIndex;
      const pageSize = request.body.pageSize;
      const includes = request.body.includes;

      response.body.totalElements = (pageIndex + 9) * pageSize;
      response.body.content = [];
      for (let i = 0; i < pageSize; i++) {
        const count = ((pageIndex - 1) * pageSize) + (i + 1);
        const item = {
          id: count,
          name: 'name-' + count,
          sub1: null,
          sub2: null
        };
        if (includes) {
          if (includes.includes('sub1')) {
            item.sub1 = {
              id: 'sub1-id-' + count,
              name: 'sub1-name-' + count
            };
          }
          if (includes.includes('sub2')) {
            item.sub2 = {
              id: 'sub2-id-' + count,
              name: 'sub2-name-' + count
            };
          }
        }

        response.body.content.push(item);
      }
    }

    return response;
  }

}
